import { TextLoader } from "langchain/document_loaders/fs/text";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import { config } from "dotenv";
import { OpenAIEmbeddings } from "@langchain/openai"; // fixed import path
import { MemoryVectorStore } from "langchain/vectorstores/memory";

await config();

const loader = new TextLoader("./data/kong.txt");
const docs = await loader.load();

const textSplitter = new RecursiveCharacterTextSplitter({
    chunkSize: 100,
    chunkOverlap: 20,
});

const splitDocs = await textSplitter.splitDocuments(docs);

console.log(splitDocs);


const embeddings = new OpenAIEmbeddings({
    apiKey: process.env.DASHSCOPE_API_KEY,
    model: "text-embedding-v1", // 阿里云的 embedding 模型
    batchSize: 25, // 阿里云限制每次最多处理 25 个文档
    configuration: {
        baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1",
    },
});

// const res = await embeddings.embedQuery(splitDocs[0].pageContent);

// console.log('res: ', res);

const vectorstore = new MemoryVectorStore(embeddings);
await vectorstore.addDocuments(splitDocs);

const retriever = vectorstore.asRetriever(3)

const res = await retriever.invoke("偷东西了吗？")

console.log('res: ', res);